﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;
using Zwms.Client.CommonUI;

namespace Zwms.Client.InWms
{
    public partial class InPlanBillDetailUC : BaseUserControl
    {
        public string CurrentInPlanBillCode { get; set; }
        public BilInPlanBillDetail CurrentSelectInPlanBillDetail { get; set; }
        public EnumOpenType OpenType { get; set; }

        public InPlanBillDetailUC()
        {
            InitializeComponent();
        }

        public InPlanBillDetailUC(EnumOpenType opentype,string inPlanBillCode)
        {
            InitializeComponent();
            OpenType = opentype;
            CurrentInPlanBillCode =  inPlanBillCode;
            CurrentSelectInPlanBillDetail = null;

            InitControl();
        }

        private void dgv_MouseClick(object sender, MouseEventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            this.CurrentSelectInPlanBillDetail = row.DataBoundItem as BilInPlanBillDetail;
        }

        #region 私有函数
        private void InitControl()
        {
            if (OpenType == EnumOpenType.New)
            {

            }
            else if (OpenType == EnumOpenType.Open)
            {
                PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
                MyDataBind(pIndex);
            }
        }

        public void LoadInPlanBillDetail(string sCode)
        {
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBillDetail queryRecord = new BilInPlanBillDetail();
            queryRecord.IBPlanCode = sCode;
            PagerIndex pagerIndex = new PagerIndex(1,int.MaxValue);
            LoadBillDetail(client.QueryInPlanBillDetailList(queryRecord,ref  pagerIndex));
        }

        private void LoadBillDetail(IList<BilInPlanBillDetail> recordList)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor; 
                this.bilInPlanBillDetailBindingSource.DataSource = null;
                if (recordList != null && recordList.Count > 0)
                {
                    this.bilInPlanBillDetailBindingSource.DataSource = recordList;
                    //this.bilInPlanBillDetailBindingSource.DataSource = new BindingCollection<BilInPlanBillDetail>(recordList);
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        public IList<BilInPlanBillDetail> GetBillDetail()
        {
            return (IList<BilInPlanBillDetail>)this.bilInPlanBillDetailBindingSource.DataSource;
        }

        private void MyDataBind(PagerIndex pIndex)
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBillDetail record = new BilInPlanBillDetail();
            record.IBPlanCode = CurrentInPlanBillCode;
            
            IList<BilInPlanBillDetail> inBillDetailList = client.QueryInPlanBillDetailList(record, ref pIndex);
            //dataPager.RecordCount = pIndex.RecordCount;
            LoadBillDetail(inBillDetailList);
        }

        public void Clear()
        {
            LoadBillDetail(null);
        }
        #endregion

     
    }
}
